package com.sgb.kjwl.viewmodel.self;


import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import androidx.databinding.ObservableField;
import android.widget.Toast;



import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.link.utils.Preferences;
import com.swgk.core.base.api.ICallBack;

import com.sgb.kjwl.model.entity.ImAccountBean;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.user.SettingInfoEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.ui.activity.self.publish.SettingActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import cn.jpush.android.api.JPushInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SettingModel extends BaseViewModel {
    private SettingActivity activity;
    private SelfRepertory repertory;

    public ObservableField<UserMsgEntity> userInfo = new ObservableField<>();

    public SettingModel(SettingActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void loginOut() {
        com.swgk.core.base.api.APIManager.getInstance().cancelOkHttpCalls(true);
        setLoginOut();
        Toast.makeText(activity, "退出登录成功", Toast.LENGTH_SHORT);
        JPushInterface.deleteAlias(activity, 1);
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_TOKEN, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PHONE, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PC_TOKEN, "");
        //TODO
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourcePCToken, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourceAPPToken, "");

        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.ENTERPRISE_AUTH, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.PERSON_AUTH, false);
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.DRIVER_AUTH, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NAME, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NO, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COM_TYPE, 0);

        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.USER_NO, "");

        activity.loginOutSucceed();
        repertory.saveToken(null);
//        NimUIKit.setAccount("");
//        AVChatKit.setAccount("");
        Preferences.saveUserAccount("");
        Preferences.saveUserToken("");
        UserOperating.getInstance().cleanCacheUser();
        MkvUtils.get().clear(MkvUtils.CacheUserCenterMenu);
    }

    private void setLoginOut() {
        APIManager.getInstance().getUserAPI().logout().enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
            }
        });
    }

    public void getSettingInfo() {
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getSettingInfo().enqueue(new Callback<BaseEntity<SettingInfoEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<SettingInfoEntity>> call, Response<BaseEntity<SettingInfoEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response == null) {
                    return;
                }
                if (response.body() == null || response.body().getState() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.refreshStatusInfo(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<SettingInfoEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getUserInfo() {
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getUserInfo().enqueue(new Callback<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserMsgEntity>> call, Response<BaseEntity<UserMsgEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) return;
                userInfo.set(response.body().getData());
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData().isRelevance()) {
                        //企业认证成功
                        activity.companySuccess(response.body().getData().getCompanyId());
                    } else {
                        //todo 测试 暂时加上店铺id
                        //activity.companyFail(response.body().getData().getEnterprise().getId());
                    }

                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<UserMsgEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public String getVersionName() {
        String versionName = "0";
        try {
            PackageInfo packageInfo = activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0);
            versionName = "V" + packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    public void setIMMobile(String mobile) {
        APIManager.getInstance().getCommonAPI().setIMMobile(mobile).enqueue(new Callback<BaseEntity<ImAccountBean>>() {
            @Override
            public void onResponse(Call<BaseEntity<ImAccountBean>> call, Response<BaseEntity<ImAccountBean>> response) {
                if (response.body() == null) return;
                MToast.showToast(activity, response.body().getData().toString());
            }

            @Override
            public void onFailure(Call<BaseEntity<ImAccountBean>> call, Throwable t) {

            }
        });
    }

//    public void getQrInfo() {
//        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
//        APIManager.getInstance().getUserAPI().getMyQrCode().enqueue(new Callback<BaseEntity<ImAccountBean>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<ImAccountBean>> call, Response<BaseEntity<ImAccountBean>> response) {
//                DialogHelper.dismissProgressDialog();
//                if (response.body() == null || response.body().getData() == null) return;
//                activity.showQRCode(response.body().getData().getImAccount());
//
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<ImAccountBean>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
//
//            }
////        });
//
//    }
}
